
package com.scj.ho;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class HolidayUtil {

    private static DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");

    public static ArrayList<HolidayBean> getAllHoliday(Context context) {
        ArrayList<HolidayBean> beans = new ArrayList<HolidayBean>();
        Date current = new Date(System.currentTimeMillis());
        HolidayBean bean = new HolidayBean();
        bean.setName(context.getString(R.string.chuxi_name));
        bean.setHaiyou(context.getString(R.string.haiyou));
        bean.setTian(context.getString(R.string.day));
        bean.setDate(context.getString(R.string.chuxi));
        bean.setNumber(daysBetween(current, getDate("2013-2-9")));
        beans.add(bean);

        HolidayBean bean1 = new HolidayBean();
        bean1.setName(context.getString(R.string.chunjie_name));
        bean1.setHaiyou(context.getString(R.string.haiyou));
        bean1.setTian(context.getString(R.string.day));
        bean1.setDate(context.getString(R.string.chunjie));
        bean1.setNumber(daysBetween(current, getDate("2013-2-10")));
        beans.add(bean1);

        HolidayBean bean2 = new HolidayBean();
        bean2.setName(context.getString(R.string.qingrenjie_name));
        bean2.setHaiyou(context.getString(R.string.haiyou));
        bean2.setTian(context.getString(R.string.day));
        bean2.setDate(context.getString(R.string.qingrenjie));
        bean2.setNumber(daysBetween(current, getDate("2013-2-14")));
        beans.add(bean2);

        HolidayBean bean3 = new HolidayBean();
        bean3.setName(context.getString(R.string.yuanxiaojie_name));
        bean3.setHaiyou(context.getString(R.string.haiyou));
        bean3.setTian(context.getString(R.string.day));
        bean3.setDate(context.getString(R.string.yuanxiaojie));
        bean3.setNumber(daysBetween(current, getDate("2013-2-24")));
        beans.add(bean3);

        return beans;
    }

    public static Date getDate(String str) {
        Date date = null;
        try {
            date = format1.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static int getCurrentDayNumber() {

        int result = 0;
        try {
            long currentDate = System.currentTimeMillis();
            Calendar currentCalendar = Calendar.getInstance();
            currentCalendar.setTimeInMillis(currentDate);

            result = currentCalendar.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }

    /**
     * 计算两个日期之间相差的天数
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetween(Date date1, Date date2) {
        int result = 0;

        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date1);
            long time1 = cal.getTimeInMillis();
            cal.setTime(date2);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);

            result = Integer.parseInt(String.valueOf(between_days));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return result;
    }
    
    
    /**
     * 分享功能。
     * @param context
     * @param uri
     */
    public static void shareText(Context context,Uri uri) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/jpeg");
        intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share_zhufu));
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra(android.content.Intent.EXTRA_TEXT,  context.getString(R.string.share_zhufu));
        context.startActivity(Intent.createChooser(intent, context.getString(R.string.share_zhufu)));
    }
}
